package com.shf.controller;

import com.github.pagehelper.PageInfo;
import com.shf.entity.Permission;
import com.shf.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.UUID;

/**
 * 资源管理 控制层
 * @author 叶枫
 */
@Controller
@RequestMapping("/admin/permission/")

public class PermissionController {
    @Autowired
    @Qualifier("permissionService")
    private PermissionService permissionService;

    @RequestMapping("findAll")
    @RolesAllowed("ROLE_ADMIN")
    public ModelAndView findAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size , ModelAndView mv){

        PageInfo<Permission> pageInfo = permissionService.findAll(page,size);

        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("permission/permission-list");
        return mv;
    }
    /**
     * 走模板
     */
    @RolesAllowed("ROLE_ADMIN")
    @RequestMapping("permission-add.do")
    public String permissionAdd(){
        return "permission/permission-add";
    }
    /**
     *
     */
    @RolesAllowed("ROLE_ADMIN")
    @RequestMapping("save")
    public String permissionSave(Permission permission ){
        permission.setId(UUID.randomUUID().toString());

        permissionService.save(permission);

        return "redirect:/admin/permission/findAll.do?page=1&size=5";
    }

    /**
     * 删除权限
     */
    @RolesAllowed("ROLE_ADMIN")
    @RequestMapping("deletePermission")
    public String deleteById(String id){

        permissionService.deleteById(id);
        return "redirect:/admin/permission/findAll.do?page=1&size=5";
    }
    @RolesAllowed("ROLE_ADMIN")
    @RequestMapping("deleteMany")
    public  String deleteMany(String [] ids){

        permissionService.deleteMany(ids);
        return "redirect:/admin/permission/findAll.do?page=1&size=5";
    }

}
